<?php
if(isset($_SESSION['user']))
{
	$user = $_SESSION['user'];
	$GetUserInformation = "SELECT * FROM b_users a, b_templates b WHERE b.templateid = a.templateclass AND a.username = '$user'";
	$GetUserInformation2 = mysql_query($GetUserInformation, $forum) or die("Error #1: " . mysql_error());
	$GetUserInformation3 = mysql_fetch_array($GetUserInformation2);
	$CurrentDate = date("U");
	$FixTime = $CurrentDate - 200;
	$UpdateRecords = "UPDATE b_users SET lasttime = '$CurrentDate' WHERE userID = '$GetUserInformation3[userID]'";
	mysql_query($UpdateRecords, $forum) or die("Error #2: " . mysql_error());
	if($GetUserInformation3['tsgone'] < $FixTime)
	{
		$UpdateUserTime = "UPDATE b_users SET tsgone = '$CurrentDate', oldtime = '$GetUserInformation3[tsgone]' WHERE userID = '$GetUserInformation3[userID]'";
		mysql_query($UpdateUserTime, $forum) or die("Error #3: " . mysql_error());
	}
	$templateclass = $GetUserInformation3['templatepath'];
}
echo "<link rel='stylesheet' href='news/style.css' type='text/css'>";

if(strlen($GetUserInformation3[username])<1)
{
	$GetUserInformation3['status'] = -1;
}

if(isset($_GET['news']) && isset($_GET['page'])) 
{
	$page = $_GET['page'];
	$news = $_GET['news'];
	$post = $_GET['post'];
	$user = $_SESSION['user'];
	$selection = "SELECT * FROM b_news WHERE id = $news";
	$selection2 = mysql_query($selection, $forum) or die("Error #4: " . mysql_error());

	echo "<table class='maintable' cellspacing='0'>";
	echo "<tr><td class='headline' colspan='6'><tr><td valign='top' class='forumrow'><div class='links2'>";
?>
<a href="javascript:get('<?php echo $sitepath; ?>news.php', 'news');" >
<?php
	echo "&nbsp;&nbsp;&nbsp;<img src='news/back-icon.gif' /> Go Back</a>";
	echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='news/addcomment.php?news=$news'><img src='news/index-icon.gif' /> Add Comment</a></div>";

	$GetForumInfo = "SELECT * FROM b_news WHERE id = '$news'";
	$GetForumInfo2 = mysql_query($GetForumInfo, $forum) or die("Error #5: " . mysql_error());
	$GetForumInfo3 = mysql_fetch_array($GetForumInfo2);
	echo "</td></tr><tr>";

	while($selection3 = mysql_fetch_array($selection2))
	{
		echo "<td valign='top' class='forumrow'>";
		$GetForumInfo3[content] = BBCode($GetForumInfo3[content]); // Parse it throught the BBCode script.
		echo "<b>$GetForumInfo3[title]</b><br><div style='font-size:11px'>$GetForumInfo3[content]</div><div class='comm'>Posted by $GetForumInfo3[author] on $getforuminfo3[datepost]&nbsp;</div>";

		$GetUserComments = "SELECT * FROM b_comments WHERE newsid = '$news' ORDER BY TIMEPOST $commorder LIMIT $page,$limit";
		$GetUserComments2=  mysql_query($GetUserComments, $forum) or die("Error #6: " . mysql_error());
		while($GetUserComments3 = mysql_fetch_array($GetUserComments2))
		{
			if ($BBCodeComments)
			{
				$GetUserComments3['content'] = BBCode($GetUserComments3['content']);
			}
			echo "<div style=' font-size: 11px; color:#ffffff'><em><strong>$GetUserComments3[poster]</strong> @ $GetUserComments3[datepost] says:</em>";
			echo "</div>";
			echo "<div style='font-size: 9px; margin-left: 8px; color:#cccccc'>$GetUserComments3[content]</div>"; 
		}
	}
	include "news/paginate.php"; 
	echo "</td></tr></table>";
}

else
{
	if($GetUserInformation3['status'] >= 3)
	{
		echo "<div class='button'><center><a href='forum/admin/addnews.php'>Add News</a></center></span></div>";
	}
	$SelectForum1 = "SELECT * FROM b_news ORDER BY TIMEPOST DESC LIMIT $newslimit";
	$SelectForum2 = mysql_query($SelectForum1, $forum) or die("Error #7: " . mysql_error());
	echo "<table class='maintable' cellspacing='0'>";
	while($SelectForum3 = mysql_fetch_array($SelectForum2))
	{
		$ico = $SelectForum3['iconid'];
		echo "<tr><td valign='center' class='forumrow' width='1%'><img src='news/id$ico.gif' /></td><td valign='center' class='forumrow'width='99%'>";
?>
<a href="javascript:get('<?php echo $sitepath; ?>news.php?news=<?php echo $SelectForum3['id']; ?>&page=0', 'news');">
<?php
		echo "<b>$SelectForum3[title]</b></a></td></tr><tr><td valign='top' class='forumrow' colspan='2'>"; 
		$SelectForum3['content'] = BBCode($SelectForum3['content']);
		echo "<div style='font-size:11px'>$SelectForum3[content]</div>";
		$CommentCount1 = "SELECT COUNT(*) FROM b_comments WHERE newsid = '$SelectForum3[id]'";
		$CommentCount2 = mysql_query($CommentCount1, $forum) or die("Error #8: " . mysql_error());
		$CommentCount = mysql_result($CommentCount2, 0); 
		echo "<div class='comm2'>";
?>
<a href="javascript:get('<?php echo $sitepath; ?>news.php?news=<?php echo $SelectForum3['id'] ?>&page=0', 'news');">
<?php
		echo "&nbsp;$CommentCount comments</a></div>";
		echo "<div class='comm'>Posted by $SelectForum3[author] on $SelectForum3[datepost]&nbsp;</div>";
		echo "</td></tr>";
	}
	if(mysql_num_rows($SelectForum2) > 0)
	{
		mysql_data_seek($SelectForum2,0);
	}
}
echo "</table>";
?>

<?php
function getstatus($statnum)
{
	if ($statnum == 0)
	{
		return "All";
	}
	else if($statnum == 1)
	{
		return "Players";
	}
	else if($statnum == 2)
	{
		return "GMs";
	}
	else if($statnum == 3)
	{
		return "Admins";
	}
	else if($statnum == 4)
	{
		return "Head Administrator";
	}
}
?> 
 
<?php
function getrank($numposts, $thequery)
{
	while($therank = mysql_fetch_array($thequery))
	{
		if($numposts >= $therank['postsneeded'])
		{ 
			$rank = $therank['rankname'];
		}
	}
	return $rank;
}
?>

<?php 
function BBCode($Text)
{
	$Text = nl2br($Text);
	$URLSearchString = " a-zA-Z0-9\:\&\/\-\?\.\=\_\~\#\'";
	$MAILSearchString = $URLSearchString . " a-zA-Z0-9\.@";
	$Text = preg_replace("(\[url\]([$URLSearchString]*)\[/url\])", '<a href="$1">$1</a>', $Text);
	$Text = preg_replace("(\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[/url\])", '<a href="$1" target="_blank">$2</a>', $Text);
	$Text = preg_replace("(\[URL\=([$URLSearchString]*)\]([$URLSearchString]*)\[/URL\])", '<a href="$1" target="_blank">$2</a>', $Text);
	$Text = preg_replace("(\[mail\]([$MAILSearchString]*)\[/mail\])", '<a href="mailto:$1">$1</a>', $Text);
	$Text = preg_replace("/\[mail\=([$MAILSearchString]*)\](.+?)\[\/mail\]/", '<a href="mailto:$1">$2</a>', $Text);
	$Text = preg_replace("(\[b\](.+?)\[\/b])is",'<b>$1</b>',$Text);
	$Text = preg_replace("(\[i\](.+?)\[\/i\])is",'<I>$1</I>',$Text);
	$Text = preg_replace("(\[u\](.+?)\[\/u\])is",'<u>$1</u>',$Text);
	$Text = preg_replace("(\[s\](.+?)\[\/s\])is",'<span class="strikethrough">$1</span>',$Text);
	$Text = preg_replace("(\[o\](.+?)\[\/o\])is",'<span class="overline">$1</span>',$Text);
	$Text = preg_replace("(\[color=(.+?)\](.+?)\[\/color\])is","<span style=\"color: $1\">$2</span>",$Text);
	$Text = preg_replace("(\[size=(.+?)\](.+?)\[\/size\])is","<span style=\"font-size: $1px\">$2</span>",$Text);
	$Text = preg_replace("/\[list\](.+?)\[\/list\]/is", '<ul class="listbullet">$1</ul>' ,$Text);
	$Text = preg_replace("/\[list=1\](.+?)\[\/list\]/is", '<ul class="listdecimal">$1</ul>' ,$Text);
	$Text = preg_replace("/\[list=i\](.+?)\[\/list\]/s", '<ul class="listlowerroman">$1</ul>' ,$Text);
	$Text = preg_replace("/\[list=I\](.+?)\[\/list\]/s", '<ul class="listupperroman">$1</ul>' ,$Text);
	$Text = preg_replace("/\[list=a\](.+?)\[\/list\]/s", '<ul class="listloweralpha">$1</ul>' ,$Text);
	$Text = preg_replace("/\[list=A\](.+?)\[\/list\]/s", '<ul class="listupperalpha">$1</ul>' ,$Text);
	$Text = str_replace("[*]", "<li>", $Text);
	$Text = preg_replace("(\[quote\](.+?)\[\/quote])is",'<center><table class="quotecode"><tr row="forumrow"><td>Q u o t e:<br>$1</td></tr></table></center>',$Text);
	$Text = preg_replace("(\[code\](.+?)\[\/code])is",'<center><table class="quotecode"><tr row="forumrow"><td>C o d e:<br>$1</td></tr></table></center>',$Text);
	$Text = preg_replace("(\[font=(.+?)\](.+?)\[\/font\])","<span style=\"font-family: $1;\">$2</span>",$Text);
	$Text = preg_replace("(\[item=(.+?),(.+?)\](.+?)\[\/item\])","<a href=\"#\" type=\"http://www.wowhead.com/?item=$1;\" style=\"color: $2;\" onclick=\"return false;\">$3</a>",$Text);
	$Text = preg_replace("/\[IMG\](.+?)\[\/IMG\]/", '<img src="$1">', $Text);
	$Text = preg_replace("/\[img\](.+?)\[\/img\]/", '<img src="$1">', $Text);
	$Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.+?)\[\/img\]/", '<img src="$3" height="$2" width="$1">', $Text);
	return $Text;
}
?>
